<?php

function daftar_karyawan_cuti($tanggal,$key){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'), 
		'nik'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'nama'=>array('style'=>'border-bottom:2px solid;width:35%'),  
		'tanggal'=>array('style'=>'text-align:center;border-bottom:2px solid;width:10%'),   
		'jatah'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),   
		'diambil'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'), 		
		'sisa'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'), 
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);	
	if(isset($_GET['key']) ){
		$query = "SELECT * FROM karyawan a 
			INNER JOIN biodata b ON a.biodata_id = b.biodata_id
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian)  
			AND b.nama LIKE '%{$key}%' OR a.nik ='{$key}'
		";
	}else{
		$query = "
			SELECT * FROM karyawan a 
			WHERE a.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable)
			AND a.karyawan_id NOT IN (select karyawan_id FROM pemberhentian)		
		";
	}	
	$query .= " ORDER BY a.nik" ;
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =15;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&tanggal={$tanggal}&key={$key}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 15; 

	list($ddtanggal , $mmtanggal , $yyyytanggal  ) = explode("-" ,$tanggal );
	$tanggal_y = $yyyytanggal ."-". $mmtanggal."-".$ddtanggal;

	while($ey = my_fetch_array($result )){
		$i++;
		$karyawan =  loaddata_karyawan($ey['karyawan_id']);  
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=detail&tanggal='.$tanggal.'&key='.$key.'&karyawan_id='.$ey['karyawan_id'], 
			'title'=>'Detail'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		$tmb=$karyawan['tmb'] ;

		$tanggal_jatah=get_tanggal_jatah($tmb,$tanggal_y)  ;
		$kelompok_cuti_id=get_kelompok_cuti_id($tmb,$tanggal_jatah) ;
		if($kelompok_cuti_id==6){
			$jatah=30;
		}else{	
			$jatah=12;
		}	
		$ambil=get_ambil_cuti($ey['karyawan_id'],$kelompok_cuti_id,date('Y-m-d',$tanggal_jatah)) ;
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),
			'nik'=>position_text_align( $karyawan['nik'], 'center'),
			'nama'=>$karyawan['nama_gelar'],  
			'tmb'=> position_text_align( date('d-m-Y',$tanggal_jatah),'center') ,
			'jatah'=>position_text_align ($jatah, 'right'),
			'ambil'=>position_text_align ($ambil, 'right'),
			'sisa'=> position_text_align ($jatah-$ambil, 'right'),
			'operasi'=> position_text_align( $detail_button , 'right'),  
		);
	}

	$datas = table_rows($row); 
	$paging = $kgPagerOBJ ->showPaging();

	$ftanggal = array(
			'name'=>'tanggal',
			'id'=>'tanggal',
			'value'=>$tanggal,
		);
		
	$search ='<form method="GET">
	<input type="hidden" name="com" value="'.$_GET["com"].'" />
	<table border="0" width="100%"> <tr>
 
	<td width="15%">Pencarian cepat</td>
	<td width="10%"><input type="text"  style="width:200px;" name="key" value="'.$key.'" />
	<td width="10%"> <input type="submit" value="GO" class="simple_search"/></td>
	<td align="right" width="65%">  	
	</td></tr></table></form>';
	
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&field='.$field.'&key='.$key.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel&field='.$field.'&key='.$key.'\';'),
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=edit&id=0\' });')
	);
	return $search.table_builder($header , $datas ,  15 ,false , $paging  );
}

function get_tanggal_jatah($tmb,$tanggal_y){
	$tahun = (int) date('Y' , strtotime($tanggal_y));
	$bulan_tmb = (int) date('m' , strtotime($tmb));
	$tgl_tmb = (int) date('d' , strtotime($tmb));
	$tanggal_tmb=$tahun.'-'.sprintf( '%02d',$bulan_tmb).'-'.sprintf( '%02d',$tgl_tmb) ;

	if($tanggal_tmb > $tanggal_y ){
		$tahun-- ;
		$tanggal_tmb=$tahun.'-'.sprintf( '%02d',$bulan_tmb).'-'.sprintf( '%02d',$tgl_tmb) ;	
	}	
	return strtotime($tanggal_tmb) ;
}	

function get_kelompok_cuti_id($tmb,$tanggal_jatah){
	$tahun_tmb = (int) date('Y' , strtotime($tmb));
	$tahun= (int) date('Y' , $tanggal_jatah);
	if($tahun==$tahun_tmb){
		return 7 ; //Cuti tahunan 
	}
	if($tahun_tmb < 2000){
		$temp= (2000-$tahun_tmb)%3 ;
		if($temp==0){
			$temp=3;
		}
		$temp=($tahun-(2000-$temp))/4;
		$temp= $temp  - (int) $temp ;
		if($temp==0){
			return 6; //Cuti Besar 
		}
	}else{	
		$temp=($tahun-$tahun_tmb)/4;
		$temp=$temp - (int) $temp ;
		if($temp==0){
			return 6; //Cuti Besar 
		}   
	}	
	return 7 ; //Cuti tahunan 
}	

function get_ambil_cuti($karyawan_id,$kelompok_cuti_id,$tanggal){
/*
	$query="SELECT * FROM surat_cuti 
		WHERE kelompok_cuti_id ={$kelompok_cuti_id} 
		AND tanggal_mulai <='{$tanggal}' 
		AND tanggal_selesai >='{$tanggal}' 
		AND karyawan_id ={$karyawan_id}";	
*/
	$query="SELECT * FROM surat_cuti 
		WHERE kelompok_cuti_id ={$kelompok_cuti_id} 
		AND (tanggal_mulai >='{$tanggal}' OR tanggal_selesai >='{$tanggal}')  
		AND karyawan_id ={$karyawan_id}";	
	$result = my_query($query);
	$ambil=0 ;
	if( my_num_rows($result) > 0 ){
		//var_dump($query); exit;	

		while($ey = my_fetch_array($result )){
			$startdate = MAX( $ey['tanggal_mulai'], $tanggal) ;	
			$tanggals = list_kalender($startdate , $ey['tanggal_selesai']  ); //tgl selesai nanti dilihat apakah  > dari tmb selanjutnya
			$tanggals = array_unique($tanggals) ;
			
			foreach($tanggals as $tgl){
				$ambil++ ;
			}
		}
	}	
	return $ambil; 
}	

function list_detail_cuti_karyawan($tanggal,$karyawan_id,$key){
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nomor Surat'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Tgl. Surat'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Dari Tanggal'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Sampai Tanggal'=>array('style'=>'border-bottom:2px solid;width:15%'),  
		'Jumlah Hari'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
		'Sisa Cuti'=>array('style'=>'text-align:right;border-bottom:2px solid;width:15%'),  
	);
	$karyawan =  loaddata_karyawan($karyawan_id);  
	$tmb=$karyawan['tmb'] ;
	$tanggal_jatah=get_tanggal_jatah($tmb,$tanggal)  ;
	$kelompok_cuti_id=get_kelompok_cuti_id($tmb,$tanggal_jatah) ;
	if($kelompok_cuti_id==6){
		$jatah=30;
		$tipe_cuti='Cuti Besar' ;
	}else{	
		$jatah=12;
		$tipe_cuti='Cuti Tahunan' ;
	}		
 
	$query="SELECT * FROM surat_cuti 
		WHERE kelompok_cuti_id ={$kelompok_cuti_id} 
	--	AND (tanggal_mulai >='{$tanggal}' OR tanggal_selesai >='{$tanggal}')  
		AND karyawan_id ={$karyawan_id}";
 	$result = my_query($query);
	$row=array();
	$i=0;
	$sisa=$jatah ;
	while($ey = my_fetch_array($result)){
		$i++;
		$ambil=0;
		//$startdate = MAX( $ey['tanggal_mulai'], $tanggal) ;	
		$startdate = $ey['tanggal_mulai'];
		$tanggals = list_kalender($startdate , $ey['tanggal_selesai']  ); //tgl selesai nanti dilihat apakah  > dari tmb selanjutnya
		$tanggals = array_unique($tanggals) ;
		
		foreach($tanggals as $tgl){
			$ambil++ ;
		}
		$sisa-=$ambil;
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nomor'=>  strtoupper($ey['nomor_surat'] ),   
			'tgl'=>  date( 'd-m-Y' , strtotime($ey['tanggal_surat']) ) ,  
			'dari'=> date( 'd-m-Y' , strtotime($ey['tanggal_mulai']) ),
			'sampai'=> date( 'd-m-Y' , strtotime($ey['tanggal_selesai'])),
			'jumlah'=> position_text_align ($ambil, 'right'), 
			'sisa'=> position_text_align ($sisa, 'right'), 
		);		 
	}
	$datas = table_rows($row); 
	
	$newline = array(
		'Tanggal Jatah Cuti'=>date('d-m-Y',$tanggal_jatah) ,
		'Tipe Cuti'=>$tipe_cuti,
		'Jatah Cuti'=>$jatah.' hari',
	);
	$pro =   karyawan_header_detail($karyawan,true, $newline  );
	$menuontop = array(  
		'Kembali' => array('onclick'=>'javascript:location.href=\'index.php?com=cuti_daftar_karyawan&key='.$key.'\''), 
	);	
	$printmenu = additional_menu_on_list( $menuontop , '');
	return  $pro.$printmenu.table_builder($header , $datas ,  6 , false   );	
}
/*
	$yearly_incomes = my_get_data_by_id('yearly_income','yearly_income_id',$yearly_income_id); 	 
	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "a" , "a"  );
	$view .= form_field_display( '<span class="label_form">'.ucfirst($yearly_incomes['label']).'</span>', "<b>Nik</b>"    ); 
	return  $view. table_builder($header , $datas ,  3 , false );
*/
